/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelos;

/**
 *
 * @author 1130608864
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;


import com.jme.bounding.BoundingBox;
import com.jme.scene.Spatial;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme; //.3ds
import java.io.FileOutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ModelConverter {

  static final FormatConverter CONVERTER_3DS = new MaxToJme();

  /**
   * Imports a .3ds model from file system. Like the 2-argument method
   * with a null textureDir. The texture file(s) are assumed to be in the
   * same directory as the model file.
   * @param modelPath the path to the model file.
   * Can be relative to the project directory.
   * @return a Spatial containing the model geometry
   * (with provided texture, if any) that can be attached to
   *  the scenegraph, or null instead if unable to load geometry.
   */
  /*public static Spatial load3ds(String modelPath) {
  return load3ds(modelPath, null);
  }*/
  /**
   * Imports a .3ds model from file system.
   * @param modelPath the path to the model file.
   * Can be relative to the project directory.
   * @param textureDir the path to the directory with the model's
   *  textures. If null, this will attempt to infer textureDir from
   *  modelPath, which assumes that the texture file(s) are in the same
   *  directory as the model file.
   * @return a Spatial containing the model geometry
   * (with provided texture, if any) that can be attached to
   *  the scenegraph, or null instead if unable to load geometry.
   */
  public static void load3ds(String modelsDirectory, String textureDir) {
    final SimpleResourceLocator location;
    try {
      location = new SimpleResourceLocator(ModelConverter.class.getClassLoader().getResource("modelos/"));
      ResourceLocatorTool.addResourceLocator(
              ResourceLocatorTool.TYPE_TEXTURE, location);
    } catch (URISyntaxException ex) {
      Logger.getLogger(ModelConverter.class.getName()).log(Level.SEVERE, null, ex);
    }
    URL modelUrl = ModelConverter.class.getClassLoader().getResource(modelsDirectory);
    File directory = new File(modelUrl.getPath().replaceAll("%20", " ") + File.separator);
    if (directory.isDirectory()) {
      final ByteArrayOutputStream outStream =
              new ByteArrayOutputStream(); // byte array streams don't have to be closed
      File[] models = directory.listFiles();
      for (int i = 0; i < models.length; i++) {
        File file = models[i];
        if (file.getName().indexOf(".3ds") > 0 || file.getName().indexOf(".3DS") > 0) {
          File checkFile = new File(file.getPath().substring(0, file.getPath().indexOf(file.getName().substring(file.getName().indexOf("."), file.getName().length()))) + ".jme");
          if (!checkFile.exists()) {
            // read .3ds file into memory & convert it to a jME usable format.
            final FileInputStream rawIn;
            try {
              rawIn = new FileInputStream(file.getPath());
              CONVERTER_3DS.convert(rawIn, outStream);
              rawIn.close(); // FileInputStream s must be explicitly closed.

              FileOutputStream modelWriter = new FileOutputStream(directory.getPath() + File.separator + file.getName().substring(0, file.getName().indexOf(".")) + ".jme");
              modelWriter.write(outStream.toByteArray());
              modelWriter.close();
              outStream.reset();
            } catch (FileNotFoundException ex) {
              Logger.getLogger(ModelConverter.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
              Logger.getLogger(ModelConverter.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
        }
      }
      /*try {
      final File textures;
      if (textureDir != null) { // set textureDir location
      textures = new File(textureDir);
      } else {// try to infer textureDir from modelPath.
      textures = new File(
      modelPath.substring(0, modelPath.lastIndexOf('/')));
      }	// Add texture URL to auto-locator
      final SimpleResourceLocator location =
      new SimpleResourceLocator(textures.toURI().toURL());
      ResourceLocatorTool.addResourceLocator(
      ResourceLocatorTool.TYPE_TEXTURE, location);
       */


      // prepare outStream for loading.

      /*} catch (FileNotFoundException e) {
      e.printStackTrace();
      System.err.println("File not found at: " + modelPath);
      } catch (IOException e) {
      e.printStackTrace();
      System.err.println("Unable read model at: " + modelPath);
      } catch (URISyntaxException e) {
      e.printStackTrace();
      System.err.println("Invalid texture location at:" + textureDir);
      }	/*
       * The bounding box is an important optimization.
       * There is no point in rendering geometry outside of the camera's
       * field of view. However, testing whether each individual triangle
       * is visible is nearly as expensive as actually rendering it. So you
       * don't test every triangle. Instead, you just test the bounding box.
       * If the box isn't in view, don't bother looking for triangles inside.
       */
      /*output.setModelBound(new BoundingBox());
      output.updateModelBound();*/
    }
  }
}
